Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: create pdf parser with unstructured #8

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

ghoullishly
Copy link

Fixes #4 : Experimented with unstructuredloader for pdf parsing. Has element_id, parent_id and document type metadata which is very useful. Coordinate metadata could be used to find images. Can (somewhat) handle math formulas which may be relevant if/when we expand to other subjects.

@ghoullishly ghoullishly requested a review from alvaro-mazcu March 5, 2025 17:06
import re
import pathlib

pdf_path = r"C:\Users\ADMIN\Desktop\KTHAIS\twiga-warehouse\data\parsed\text.md"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path should be relative inside the project, so that every developer has its own absolute path

("#####", "Header 4"), # Bold + italic
]

def preprocess_md(md_doc):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you type the input and the output? Example:

def foo(im_a_number: int) -> int:
    return im_a_number + 1

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for other functions

Comment on lines +39 to +40
chunk_size = 250
chunk_overlap = 30
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it would be interesting to declare these two variables as input of the functions so that we could play with them if needed. Example:

def recursive_split(md_header_splits, chunk_size: int = 250, chunk_overlap: int = 30):
    """Split document recursively."""
    ...

Comment on lines +50 to +72
md_doc = preprocess_md(md_doc)

# Split data
md_header_splits = md_split(md_doc)
#character_splits = recursive_split(md_header_splits)

# Append metadata
splits_data = []
for split in md_header_splits:
splits_data.append({
"content": split.page_content,
"metadata": split.metadata
})

# Save JSON output
output_dir = pathlib.Path(r"C:\Users\ADMIN\Desktop\KTHAIS\twiga-warehouse\data\parsed")
output_dir.mkdir(exist_ok=True)

output_json_path = output_dir / "text.json"
with open(output_json_path, "w", encoding="utf-8") as json_file:
json.dump(splits_data, json_file, ensure_ascii=False, indent=4)

print(f"Markdown splits saved to {output_json_path}")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this inside a main function? This also implies creating the if __name__ == "__main__": famous line at the bottom of the file. Same for the other files

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

try unstructuredloader to parse a geography textbook
2 participants